<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Qiniu extends Auth_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('goods_mdl');
        $this->load->model('image_mdl');

        //注意，七牛云存储的配置参数（AK/SK）需要在sdk里面去设置
        $this->load->library('qiniu_sdk');
    }

    public function index(){
        $gid = $this->input->get('gid',TRUE);
        if(empty($gid)){
            //show_404();
            //exit;
            $gid = 9;
        }

        $this->db->where('id',intval($gid));
        $res = $this->goods_mdl->get_one();
        $data['res'] = $res;

        if(empty($res)){
            show_error('参数错误！');
        }

        //查询图片

        $this->db->where('goods_id',$gid);
        $this->db->order_by('sort DESC,id ASC');
        $data['list'] = $this->image_mdl->select();

        $this->_view('admin/qiniu_index',$data);
    }

    /**
     * 文件上传
     */
    public function update(){
        //$res = $this->qiniu_sdk->stat('zaibuzai_2015-08-26-1200475435.png');
        if(empty($_FILES['smallimg'])){
            die('请选择文件上传！');
        }
        if (!empty($_FILES['smallimg']['error'])) {
            die('请选择文件上传！');
        }

        if(!in_array($_FILES['smallimg']['type'], array('image/jpg', 'image/jpeg', 'image/gif', 'image/png'))) 
        {
            echo '请上传正确的图片格式，支持 jpg | jpeg | gif | png';
        }
        $gid = $this->input->post('gid');
        $gid = intval($gid);
        if(empty($gid)){
            die('参数错误！');
        }

        //判断图片是否超过7张
        $this->db->where('goods_id',$gid);
        $num = $this->image_mdl->img_count();
        if($num >= 7){
            die('每件商品最多只能上传7张图片');
        }

        $file_name = $gid.'-'.$this->session->id.'-'.date('YmdHis').'-'.rand(0,99999);
        $res = $this->qiniu_sdk->upload($_FILES['smallimg']['tmp_name'],$file_name);

        if(empty($res['hash'])){
            //发送邮件给管理员
            $mess = "商品编号".$gid."，上传图片失败，没有返回值";
            $this->email_admin($mess);
            die('上传数据失败，请联系网站管理员！');
        }

        $data = array(
            'goods_id' => $gid ,
            'name' => $this->input->post('title',TRUE) ,
            'file_name' => $res['key'],
            'path' => 'http://7xlakk.com1.z0.glb.clouddn.com/' ,
            'thumb' => $res['key'].'?imageView2/5/w/300/h/300/q/80/format/png',
        );

        $res = $this->image_mdl->create($data);
        echo $res === false ? '保存失败，请重新上传' : 1 ;
    }

    /**
     * 图片删除
     */
    public function del_image(){
        if(!$this->input->is_ajax_request()){
            $this->error('参数错误！');
        }
        $iid = $this->input->post('iid',TRUE);
        $iid = intval($iid);

        $this->db->where('id',$iid);
        $result = $this->image_mdl->get_one();

        if(empty($result['file_name'])){
            die('参数错误!');
        }

        //删除文件
        $this->qiniu_sdk->delete($result['file_name']);
        //删除数据库数据
        $this->db->where('id',$iid);
        $res = $this->image_mdl->delete();
        echo $res === false ? '删除数据失败！' : '1';
    }

}

/* End of file Qiniu.php */
/* Location: ./application/controllers/admini/Qiniu.php */